<?php
/* Begin Widget Class */
class DT_contact_info extends WP_Widget {

	/* Widget setup  */
	function DT_contact_info() {
		/* Widget settings. */
		$widget_ops = array(
            'classname'     => 'dt_contact_info',
            'description'   => __('Contact info', LANGUAGE_ZONE)
        );

		/* Widget control settings. */
		$control_ops = array(
            'width'     => 250,
            'height'    => 250,
            'id_base'   => 'dt_contact_info'
        );

		/* Create the widget. */
		$this->WP_Widget(
            'dt_contact_info',
            __( THEME_TITLE.' Contact Info', LANGUAGE_ZONE ),
            $widget_ops,
            $control_ops
        );
	}

	/* Display the widget  */
	function widget( $args, $instance ) {
		extract( $args );
        
        $defaults = array(
            'map_code'  => '',
            'address'   => '',    
            'phone'     => '',    
            'mobile'    => '',    
            'fax'       => '',    
            'skype'     => '',    
            'email'     => '',    
        );
        $instance = wp_parse_args( $instance, $defaults );
        
        $title = apply_filters('widget_title', $instance['title'] );
        
        $dt_content = '%s';
        if( isset($id) && 'bottom-widget-area' != $id ) {
            $dt_content = '<div class="widget">%s</div><div class="widget_b"></div>';
        }
        
        // output
        echo $before_widget ;
        
        if( $title ) {
            echo $before_title . $title . $after_title;
        }
        
        ob_start();
        if( $instance['address'] ):
        ?>
            <p>
                <span class="mid_gray"><?php _e( 'Address:', LANGUAGE_ZONE ); ?></span><br/>
                <?php echo $instance['address']; ?>
            </p>
        <?php
        endif;
        
        if( $instance['phone'] ):
        ?>
            <p>
                <span class="mid_gray"><?php _e( 'Phone:', LANGUAGE_ZONE ); ?></span><br/>
                <?php echo $instance['phone']; ?>
            </p>
        <?php
        endif;
        
        if( $instance['mobile'] ):
        ?>
            <p>
                <span class="mid_gray"><?php _e( 'Mobile:', LANGUAGE_ZONE ); ?></span><br/>
                <?php echo $instance['mobile']; ?>
            </p>
        <?php
        endif;
        
        if( $instance['fax'] ):
        ?>
            <p>
                <span class="mid_gray"><?php _e( 'Fax:', LANGUAGE_ZONE ); ?></span><br/>
                <?php echo $instance['fax']; ?>
            </p>
        <?php
        endif;
        
        if( $instance['skype'] ):
        ?>
            <p>
                <span class="mid_gray"><?php _e( 'Skype:', LANGUAGE_ZONE ); ?></span><br/>
                <?php echo $instance['skype']; ?>
            </p>
        <?php
        endif;
        
        if( $instance['email'] ):
        ?>
            <p>
                <span class="mid_gray"><?php _e( 'E-mail:', LANGUAGE_ZONE ); ?></span><br/>
                <?php echo $instance['email']; ?>
            </p>
        <?php
        endif;
        
        if( $instance['map_code'] ):
        ?>
            <span class="mid_gray"><?php _e( 'Map:', LANGUAGE_ZONE ); ?></span><br/>
            <div class="map">
            <?php echo $instance['map_code']; ?>
            </div>
        <?php
        endif;
        
        $html = ob_get_clean();
        
        printf(
            $dt_content,
            $html
        );
        
        echo $after_widget;
	}

	/* Update the widget settings  */
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
        
        $instance['title'] = $new_instance['title'];
        $instance['map_code'] = $new_instance['map_code'];
        $instance['address'] = $new_instance['address'];
        $instance['phone'] = $new_instance['phone'];
        $instance['mobile'] = $new_instance['mobile'];
        $instance['fax'] = $new_instance['fax'];
        $instance['skype'] = $new_instance['skype'];
        $instance['email'] = $new_instance['email'];
        
		return $instance;
	}

	/**
	 * Displays the widget settings controls on the widget panel.
	 * Make use of the get_field_id() and get_field_name() function
	 * when creating your form elements. This handles the confusing stuff.
	 */
	function form( $instance ) {

		/* Set up some default widget settings. */
		$defaults = array(
            'title'     => '',
            'map_code'  => '',
            'address'   => '',    
            'phone'     => '',    
            'mobile'    => '',    
            'fax'       => '',    
            'skype'     => '',    
            'email'     => '',    
        );

		$instance = wp_parse_args( (array) $instance, $defaults );
    ?>
		
		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', LANGUAGE_ZONE); ?></label>
			<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:85%;" />
		</p>
        
        <p>
			<label for="<?php echo $this->get_field_id( 'address' ); ?>"><?php _e('Address:', LANGUAGE_ZONE); ?></label>
			<input id="<?php echo $this->get_field_id( 'address' ); ?>" name="<?php echo $this->get_field_name( 'address' ); ?>" value="<?php echo $instance['address']; ?>" style="width:85%;" />
		</p>
        
        <p>
			<label for="<?php echo $this->get_field_id( 'phone' ); ?>"><?php _e('Phone:', LANGUAGE_ZONE); ?></label>
			<input id="<?php echo $this->get_field_id( 'phone' ); ?>" name="<?php echo $this->get_field_name( 'phone' ); ?>" value="<?php echo $instance['phone']; ?>" style="width:85%;" />
		</p>
        
        <p>
			<label for="<?php echo $this->get_field_id( 'mobile' ); ?>"><?php _e('Mobile:', LANGUAGE_ZONE); ?></label>
			<input id="<?php echo $this->get_field_id( 'mobile' ); ?>" name="<?php echo $this->get_field_name( 'mobile' ); ?>" value="<?php echo $instance['mobile']; ?>" style="width:85%;" />
		</p>
        
        <p>
			<label for="<?php echo $this->get_field_id( 'fax' ); ?>"><?php _e('Fax:', LANGUAGE_ZONE); ?></label>
			<input id="<?php echo $this->get_field_id( 'fax' ); ?>" name="<?php echo $this->get_field_name( 'fax' ); ?>" value="<?php echo $instance['fax']; ?>" style="width:85%;" />
		</p>
        
        <p>
			<label for="<?php echo $this->get_field_id( 'skype' ); ?>"><?php _e('Skype:', LANGUAGE_ZONE); ?></label>
			<input id="<?php echo $this->get_field_id( 'skype' ); ?>" name="<?php echo $this->get_field_name( 'skype' ); ?>" value="<?php echo $instance['skype']; ?>" style="width:85%;" />
		</p>
        
        <p>
			<label for="<?php echo $this->get_field_id( 'email' ); ?>"><?php _e('Email:', LANGUAGE_ZONE); ?></label>
			<input id="<?php echo $this->get_field_id( 'email' ); ?>" name="<?php echo $this->get_field_name( 'email' ); ?>" value="<?php echo $instance['email']; ?>" style="width:85%;" />
		</p>
        
        <p>
			<label for="<?php echo $this->get_field_id( 'map_code' ); ?>"><?php _e('Map code:', LANGUAGE_ZONE); ?></label>
			<textarea id="<?php echo $this->get_field_id( 'map_code' ); ?>" name="<?php echo $this->get_field_name( 'map_code' ); ?>" style="width:202px;"><?php echo $instance['map_code']; ?></textarea>
		</p>
				
		<div style="clear: both;"></div>
	<?php
	}
}

function dt_vontact_info_register() {
	register_widget( 'DT_contact_info' );
}

add_action( 'widgets_init', 'dt_vontact_info_register' );
